﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.Models;
using Server.BaseService;

namespace Server.BaseManage
{
    public class BaseManageBLL
    {
        BaseManageDAL bDal = new BaseManageDAL();
        #region 商品管理
        public void CommoidtyAdd(CommodityModel cm)
        {
            try
            {
                bDal.CommoidtyAdd(cm);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void CommoidtyEdit(CommodityModel cm)
        {
            try
            {
                bDal.CommoidtyEdit(cm);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CommoidtyDel(int id)
        {
            try
            {
                bDal.CommoidtyDel(id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public CommodityModel[] GetCommidityByPage(string PageNum)
        {
            try
            {
                var dt = bDal.GetCommidityByPage(PageNum);
                CommodityModel[] cm = new CommodityModel[dt.Rows.Count];
                for (int i = 0; i < cm.Length; i++)
                {
                    var dr = dt.Rows[i];
                    cm[i] = new CommodityModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        uid = Convert.ToInt32(dr["uid"]),
                        CName = dr["CName"].ToString(),
                        ParentCode = dr["ParentCode"].ToString(),
                        Code = dr["Code"].ToString(),
                        SimpCode = dr["SimpCode"].ToString(),
                        Models = dr["Models"].ToString(),
                        Unit = dr["Unit"].ToString(),
                        TaxCode = dr["TaxCode"].ToString(),
                        Discount = Convert.ToInt32(dr["Discount"]),
                        TaxItem = dr["TaxItem"].ToString(),
                        Tax = dr["Tax"].ToString(),
                        Price = Convert.ToDecimal(dr["Price"]),
                        TaxMark = Convert.ToInt32(dr["TaxMark"]),
                        TaxName = dr["TaxName"].ToString(),
                        DiscountCate = dr["DiscountCate"].ToString()
                    };
                }
                return cm;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public CommodityModel[] GetCommidityByLikeName(string LikeName, int uid)
        {
            try
            {
                var dt = bDal.GetCommidityByLikeName(LikeName, uid);
                CommodityModel[] cm = new CommodityModel[dt.Rows.Count];
                for (int i = 0; i < cm.Length; i++)
                {
                    var dr = dt.Rows[i];
                    cm[i] = new CommodityModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        uid = Convert.ToInt32(dr["uid"]),
                        CName = dr["CName"].ToString(),
                        ParentCode = dr["ParentCode"].ToString(),
                        Code = dr["Code"].ToString(),
                        SimpCode = dr["SimpCode"].ToString(),
                        Models = dr["Models"].ToString(),
                        Unit = dr["Unit"].ToString(),
                        TaxCode = dr["TaxCode"].ToString(),
                        Discount = Convert.ToInt32(dr["Discount"]),
                        TaxItem = dr["TaxItem"].ToString(),
                        Tax = dr["Tax"].ToString(),
                        Price = Convert.ToDecimal(dr["Price"]),
                        TaxMark = Convert.ToInt32(dr["TaxMark"]),
                        TaxName = dr["TaxName"].ToString(),
                        DiscountCate = dr["DiscountCate"].ToString()
                    };
                }
                return cm;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 客户管理
        public void CustomerAdd(CustomerModel cm)
        {
            try
            {
                cm.Code = bDal.GetCode();
                bDal.CustomerAdd(cm);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CustomerEdit(CustomerModel cm)
        {
            try
            {
                cm.Code = bDal.GetCode();
                bDal.CustomerEdit(cm);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CustomerDel(int id)
        {
            try
            {
                bDal.CustomerDel(id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public CustomerModel[] GetCustomerByPage(string PageNum)
        {
            try
            {
                var dt = bDal.GetCustomerByPage(PageNum);
                CustomerModel[] cm = new CustomerModel[dt.Rows.Count];
                for (int i = 0; i < cm.Length; i++)
                {
                    var dr = dt.Rows[i];
                    cm[i] = new CustomerModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        ParentCode = dr["ParentCode"].ToString(),
                        Code = dr["Code"].ToString(),
                        SimpCode = dr["Code"].ToString(),
                        CName = dr["CName"].ToString(),
                        TaxCode = dr["TaxCode"].ToString(),
                        Email = dr["Email"].ToString(),
                        Address_Tel = dr["Address_Tel"].ToString(),
                        Bank_Number = dr["Bank_Number"].ToString(),
                        Remark = dr["Remark"].ToString(),
                    };
                }
                return cm;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public CustomerModel[] GetCustomerByLikeName(string LikeName, int uid)
        {
            try
            {
                var dt = bDal.GetCustomerByLikeName(LikeName, uid);
                CustomerModel[] cm = new CustomerModel[dt.Rows.Count];
                for (int i = 0; i < cm.Length; i++)
                {
                    var dr = dt.Rows[i];
                    cm[i] = new CustomerModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        ParentCode = dr["ParentCode"].ToString(),
                        Code = dr["Code"].ToString(),
                        SimpCode = dr["Code"].ToString(),
                        CName = dr["CName"].ToString(),
                        TaxCode = dr["TaxCode"].ToString(),
                        Email = dr["Email"].ToString(),
                        Address_Tel = dr["Address_Tel"].ToString(),
                        Bank_Number = dr["Bank_Number"].ToString(),
                        Remark = dr["Remark"].ToString(),
                    };
                }
                return cm;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
        #region 获取广告
        public AdsManageModel[] GetAdsList()
        {
            try
            {
                return bDal.GetAdsList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }
}
